package com.kangda.parttitionstorage.part;

import static android.os.Environment.MEDIA_MOUNTED;

import android.os.Environment;

import java.io.File;

/**
 * @Author Kang da
 * @Date 2021/12/1 3:06 下午
 */
public class PartStaticFinalUtil {

    public static final String DEFAULT_PATH = "AppName";

    public static File getFileDirByType(String mimeType) throws Exception {
        String externalStorageState;
        try {
            externalStorageState = Environment.getExternalStorageState();
        } catch (NullPointerException | IncompatibleClassChangeError e) {
            throw new Exception("未找到外部存储设备");
        }

        if (!MEDIA_MOUNTED.equals(externalStorageState)) {
            throw new Exception("外部存储状态异常");
        }

        File file;
        if (mimeType.contains("image/")) {
            file = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES);
            file = new File(file, DEFAULT_PATH);
        } else if (mimeType.contains("video/")) {
            file = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_MOVIES);
            file = new File(file, DEFAULT_PATH);
        } else if (mimeType.contains("audio/")) {
            file = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_MUSIC);
            file = new File(file, DEFAULT_PATH);
        } else {
            file = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS);
            file = new File(file, DEFAULT_PATH);
        }
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }
}